<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/4/24
 * Time: 8:54
 */

namespace app\admin\model;

use app\first\model\Notice;
use think\Db;

class Dream extends Base {
    /**
     * @param array $where
     * @param string $order
     * @param int $limit
     * @param string $field
     * @param bool $out
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($where = [], $order = '', $limit = 10, $field = '', $out = false){
        $list = $this
            ->field($field,$out)
            ->with(['member','info'])
            ->where($where)
            ->order($order)
            ->paginate($limit);
        $page = $list->currentPage();
        $total = $list->total();
        foreach ($list as $k => $v){
            $list[$k]['sort_asc'] = $k + 1 + ($page-1) * $limit;
            $list[$k]['sort_desc'] = $total - ($page-1) * $limit - $k;
        }
        return $list;
    }

    /**
     * 审核
     * @param $id
     * @param int $type
     * @param string $info
     * @param null $out
     * @return bool
     */
    public function check($id,$type=1,$info='',&$out=null){
        $_info = $this->find($id);
        if(!$_info){
            $out = '申请不存在';
            return false;
        }
        if($_info['status']!=1){
            $out = '申请处理了';
            return false;
        }
        if($type==2){
            if(empty($info)){
                $out = '请输入拒绝理由';
                return false;
            }
            $title= "审核失败";
            $_info->info = $info;
            $_status = 3;
            $content = "抱歉！您发布的{$_info['title']}未通过审核！";
        }else{
            $_status = 2;
            $title= "审核成功";
            $content = "恭喜您！您发布的{$_info['title']}通过了审核！";
            $end = date('Y-m-d',time() + 60 * 60 * 24 * 7);
            $_info->end_time = strtotime("{$end} 23:59:59");
        }
        $_info->status = $_status;
        $_info->check_time = time();
        Db::startTrans();
        $result = $_info->save();
        $result2 = (new Notice())->addNew($_info['mid'],$title,$content,2,$_info['cid_type'],$_info['id']);
        if($result && $result2) {
            $_data = $_info->toArray();
            $_data['sql'] = $_info->getLastSql();
            $_data['msg']=$content;
            $out = $_data;
            Db::commit();
            return true;
        }else{
            $out = '操作失败';
            Db::rollback();
            return false;
        }
    }

    public function getOneById($id = 0, $field = '*'){
        $info = $this
            ->field($field)
            ->with('admin')
            ->find($id);
        return $info;
    }

    public function add($data){
        $this->save($data);
    }

    public function member(){
        return $this->hasOne('Member','id','mid')
            ->bind([
                'account',
                'nickname'
            ]);
    }

    public function info(){
        return $this->hasOne('ArticleInformation','aid','id');
    }

    /**
     * @param $value
     * @return array|bool|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getImageAttr($value){
        if(empty($value)){
            return '';
        }
        $pic = new Pic();
        return $pic->getUrlsById(explode(',',$value));
    }
}